/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gob.fonafe.dao.impl.stored;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import oracle.jdbc.driver.OracleTypes;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
import pe.gob.fonafe.domain.LoginBean;
import pe.gob.fonafe.domain.UsuarioBean;

/**
 *
 * @author Irving
 */
public class ValidarUsuarioSP extends StoredProcedure{

    private static final String PROC_PARAM ="USERADMIN.P_VALIDAUSUARIO";
    
    public ValidarUsuarioSP(DataSource ds) {
        super(ds,PROC_PARAM);
        declareParameter(new SqlParameter("p_LOGIN", Types.VARCHAR));
        declareParameter(new SqlParameter("P_PASSWORD", Types.VARCHAR));
        declareParameter(new SqlOutParameter("cCursor", OracleTypes.CURSOR,new ObtenerUsuarioRowMapper()));
        
    }
    
      public Map execute(LoginBean loginBean) {
        Map inputs = new HashMap();
        inputs.put("p_LOGIN", loginBean.getLogin());
        inputs.put("P_PASSWORD", loginBean.getPassword());
        return super.execute(inputs);
    }
    
    
    private static class ObtenerUsuarioRowMapper implements RowMapper<UsuarioBean>{

        public UsuarioBean mapRow(ResultSet rs, int rowNum) throws SQLException {
            UsuarioBean bean = new UsuarioBean();
            bean.setId_usuario(rs.getInt("ID_USUARIO"));
            bean.setDniUsuario(rs.getString("DNIUSUARIO"));
            bean.setNombre(rs.getString("NOMBRE"));
            bean.setApe_Paterno(rs.getString("APE_PATERNO"));
            bean.setApe_Materno(rs.getString("APE_MATERNO"));
            bean.setEmail(rs.getString("EMAIL"));
            bean.setFecha_nacimiento(rs.getDate("FECHA_NACIMIENTO"));
            bean.setTb_Perfil_id_perfil(rs.getInt("TB_PERFIL_ID_PERFIL"));
            bean.setTb_Empresa_id_empresa(rs.getInt("tb_empresa_id_empresa"));
            return bean;
        }
    }
    
}
